package com.hlm.transparent.service;

import com.hlm.transparent.core.check.CJT188Check;
import com.hlm.transparent.core.check.ModBusCheck;
import com.hlm.transparent.core.command.CJT188Command;
import com.hlm.transparent.core.command.MODBUSCommand;
import com.hlm.transparent.core.resolve.CJT188Resolve;
import com.hlm.transparent.model.SocketMap;
import com.hlm.transparent.model.TransparentData;
import com.hlm.transparent.util.HexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;

/**
 * @program:transparent
 * @description:service
 * @author: xinY
 **/
@Service
public class ProtocolService  {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Value("${socket.server.host}")
    private String host;
    @Value("${socket.server.port}")
    private Integer port;
    public void excute(String addr,String protocolCode){
        // 判断协议类型 CJT188
        ByteBuf commandBuf = null;
        if(protocolCode.startsWith("C")){
            commandBuf = CJT188Command.packages(addr,protocolCode);
        }
        // modbus
        if (protocolCode.startsWith("m")){
            commandBuf = MODBUSCommand.packages(addr,protocolCode);
        }
        // EN1434
        if (protocolCode.startsWith("E")){

        }
        for(int i = 0 ; i < 3 ; i ++){
            // 发送
            send(commandBuf);
            try {
                Thread.sleep(50*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void send(ByteBuf byteBuf) {
        // TODO: 2020/8/22  
    }
}
